package org.mtmi.io.controls;

import java.io.IOException;

import net.n3.nanoxml.IXMLElement;

import org.mtmi.io.ReaderError;
import org.mtmi.io.SceneReader;
import org.mtmi.io.SceneWriter;
import org.mtmi.io.XMLReader;
import org.mtmi.io.XMLWriter;
import org.mtmi.ui.controls.AbstractRangeValueControl;
import org.mtmi.ui.controls.Control;
import org.mtmi.ui.controls.SKnob;

/**
 * In addition to {@link AbstractRangeValueControl}, save {@link SKnob} features:
 * <ul>
 * <li> the background image.
 * <li> the knob image.
 * </ul>
 */
public class SKnobIO extends RangeValueControlIO
{
	
	private static final String E_IMAGES ="images"; //$NON-NLS-1$
	private static final String A_BG = "background"; //$NON-NLS-1$
	private static final String A_KB = "knob"; //$NON-NLS-1$

	@Override
	public void writeControl( Control c, XMLWriter w, SceneWriter sw ) throws IOException 
	{
		super.writeControl(c, w, sw);
		
		//images
		SKnob sknob = (SKnob)c;
		w.addElement( E_IMAGES, A_BG, sknob.getBackgroundImage(), A_KB, sknob.getKnobImage() ); 
	}
	
	@Override
	public void readControl( Control c, IXMLElement element, XMLReader r, SceneReader sr ) throws ReaderError 
	{
		super.readControl(c, element, r, sr);
		
		//images:		
		IXMLElement e = r.getElement( element, E_IMAGES, true );
		String bg = r.getAttrString( e, A_BG ); 
		String kb = r.getAttrString( e, A_KB );
		
		SKnob knob = (SKnob)c;
		knob.setImages( bg, kb );
	}
}
